package com.dxd.servlet.swzl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dxd.constant.ErrorCodeConstant;
import com.dxd.entity.Message;
import com.dxd.entity.MessageBoard;
import com.dxd.entity.MessageType;
import com.dxd.entity.ReturnBean;
import com.dxd.entity.UserInfo;
import com.dxd.servlet.PublicServlet;
import com.dxd.utils.JSONTools;

/**
 * 前端用户管理
 * 
 * @author pppcar
 *
 */
@WebServlet("/auth/swzl/message")
public class MessageServlet extends PublicServlet {

	private static final long serialVersionUID = -2754603844053914575L;
	private static final Logger LOG = LoggerFactory.getLogger(MessageServlet.class);

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String type = request.getParameter("type");
		if ("submitBoard".equals(type)) {

			// 留言
			ReturnBean returnBean = ReturnBean.returnBeanBuild();

			// 获取消息id
			String messageId = request.getParameter("messageId");
			if (messageId == null || "".equals(messageId)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("消息id为空，请刷新重试");
				returnJson(returnBean);
				return;
			}
			// 查询消息是否存在，并且判断状态
			Message message = messageDao.queryOne(Integer.parseInt(messageId));
			if (message == null || message.getId() == null || message.getState() == 3 || message.getState() == 4) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("消息不存在或已经关闭");
				returnJson(returnBean);
				return;
			}
			// 获取当前登录用户
			UserInfo userInfo = getUserInfoSwzl();

			// 获取朋友id,如果为空就取message创建人
			String firendIdStr = request.getParameter("firendId");
			Integer firendId = null;
			if (firendIdStr == null || "".equals(firendIdStr)) {
				firendId = message.getCreateUse().getId();
			} else {
				firendId = Integer.parseInt(firendIdStr);
			}

			// 获取回复内容
			String content = request.getParameter("content");
			if (content == null || "".equals(content)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("内容不能为空");
				returnJson(returnBean);
				return;
			}

			// 获取前缀，保存的时候把前缀去掉
			String prefix = request.getParameter("prefix");
			if (prefix != null && !"".equals(prefix)) {
				content = content.replaceAll(prefix, "");
			}

			// 获取parentId
			String parentIdStr = request.getParameter("parentId");
			Integer parentId = 0;
			if (parentIdStr != null && !"".equals(parentIdStr)) {
				parentId = Integer.parseInt(parentIdStr);
			}

			// 创建留言板对象，保存留言
			MessageBoard messageBoard = new MessageBoard();
			messageBoard.setContent(content);
			messageBoard.setMessageId(message.getId());
			UserInfo firend = new UserInfo();
			firend.setId(firendId);
			messageBoard.setFirend(firend);
			messageBoard.setMessage(message);
			messageBoard.setParentID(parentId);
			messageBoard.setState(1);
			messageBoard.setUser(userInfo);
			messageBoardDao.addMessageBoard(messageBoard);
			LOG.info("留言成功，内容：{}", content);

			returnBean.setReturnMsg("留言成功");
			returnJson(returnBean);
			return;

		} else if ("goReleaseMessage".equals(type)) {

			// 跳转到发布消息页面
			Dispatcher("/view/swzl/releaseMessage.jsp");

		} else if ("releaseMessage".equals(type)) {

			// 发布消息
			ReturnBean returnBean = ReturnBean.returnBeanBuild();

			// 消息图片
			String messageImgs = request.getParameter("messageImgs");
			if (messageImgs == null || "".equals(messageImgs)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请上传消息图片");
				returnJson(returnBean);
				return;
			}

			// 消息标题
			String title = request.getParameter("title");
			if (title == null || "".equals(title)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请填写标题");
				returnJson(returnBean);
				return;
			}

			// 物品类型
			String goodsType = request.getParameter("goodsType");
			if (goodsType == null || "".equals(goodsType)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请选择物品类型");
				returnJson(returnBean);
				return;
			}

			// 拾物/失物地址
			String address = request.getParameter("address");
			if (address == null || "".equals(address)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请填写失物/拾物地址");
				returnJson(returnBean);
				return;
			}

			// 拾物/失物时间
			String lostTime = request.getParameter("lostTime");
			if (lostTime == null || "".equals(lostTime)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请填写失物/拾物时间");
				returnJson(returnBean);
				return;
			}

			// 详细内容
			String detailContent = request.getParameter("detailContent");
			if (detailContent == null || "".equals(detailContent)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请填写详细内容");
				returnJson(returnBean);
				return;
			}

			// 创建消息对象并保存
			Message message = new Message();
			message.setAddress(address);
			UserInfo user = getUserInfoSwzl();// 获取当前登录用户
			message.setCreateUse(user);
			message.setDetailContent(detailContent);
			StringBuffer imgs = new StringBuffer();
			String[] messageImgStrs = messageImgs.split(",");
			for (String str : messageImgStrs) {
				String[] headImgs = str.split("\\/");
				imgs.append(headImgs[headImgs.length - 1]);
				imgs.append(",");
			}
			message.setImgs(imgs.substring(0, imgs.length() - 1));
			message.setLostTime(lostTime);
			MessageType messageType = new MessageType();
			messageType.setId(Integer.parseInt(goodsType));
			message.setMessageType(messageType);
			message.setState(0);// 刚发布状态都是待审核状态
			message.setTitle(title);
			messageDao.addMessage(message);
			LOG.info("发布成功，内容：{}", JSONTools.obj2json(message));

			returnBean.setReturnMsg("发布成功");
			returnJson(returnBean);
			return;
		}else if("updateState".equals(type)){
			ReturnBean returnBean=ReturnBean.returnBeanBuild();
			String id=request.getParameter("id");
			String state=request.getParameter("state");
			if(id==null){
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("id为空");
				returnJson(returnBean);
				return;
			}
			if(state==null){
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请选择状态");
				returnJson(returnBean);
				return;
			}
			messageDao.updateState(id, state);
			returnBean.setReturnMsg("修改成功");
			returnJson(returnBean);
			return;
			
		}
	}

}